import { defHttp } from '/@/common/util/axios';
import { Modal } from 'ant-design-vue';
import { isObject } from '/@/common/util/is';
enum Api {
  getUserPage = '/sys/user/getPage',
  save = '/sys/user/add',
  edit = '/sys/user/edit',

  getRoleByUserId = '/sys/user/getRoleByUserId',
  getPostByUserId = '/sys/user/getPostByUserId',
  deleteUser = '/sys/user/delete',
  deleteBatch = '/sys/user/deleteBatch',
  importExcel = '/sys/user/importExcel',
  exportXls = '/sys/user/exportXls',
  getRecycleUser = '/sys/user/getRecycleUser',
  putRecycleUser = '/sys/user/putRecycleUser',
  deleteRecycleBin = '/sys/user/deleteRecycleBin',
  changePassword = '/sys/user/updatePassword',
  updateStatus = '/sys/user/updateStatus',
  userQuitAgent = '/sys/user/userQuitAgent',
  getQuitList = '/sys/user/getQuitList',
  putCancelQuit = '/sys/user/putCancelQuit',
  
  duplicateCheck = '/sys/common/fieldCheck', 
  allRolesList = '/sys/role/getList',
  allTenantList = '/sys/tenant/queryByList',
}
/**
 * 导出api
 * @param params
 */
export const getExportUrl = Api.exportXls;
/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;

/**
 * 用户分页
 * @param params
 */
export const getUserPage = (params) => defHttp.get({ url: Api.getUserPage, params });

/**
 * 用户角色接口
 * @param params
 */
export const getRoleByUserId = (params) => defHttp.get({ url: Api.getRoleByUserId, params }, { errorMessageMode: 'none' });


/**
 * 用户职务接口
 * @param params
 */
export const getPostByUserId = (params) => defHttp.get({ url: Api.getPostByUserId, params }, { errorMessageMode: 'none' });

/**
 * 删除用户
 */
export const deleteUser = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.deleteUser, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
};
/**
 * 批量删除用户
 * @param params
 */
export const batchDeleteUser = (params, handleSuccess) => {
  Modal.confirm({
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    },
  });
};
/**
 * 保存或者更新用户
 * @param params
 */
export const saveOrUpdateUser = (params, isUpdate) => {
  let url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({ url: url, params });
};

/**
 * 20231215
 * liaozhiyang
 * 唯一校验（ 延迟【防抖】）
 * @param params
 */
const timer = {};
export const duplicateCheckDelay = (params) => {
  return new Promise((resove, rejected) => {
    let key;
    if (isObject(params)) {
      key = `${params.tableName}_${params.fieldName}`;
    } else {
      key = params;
    }
    clearTimeout(timer[key]);
    timer[key] = setTimeout(() => {
      defHttp
        .get({ url: Api.duplicateCheck, params }, { isTransformResponse: false })
        .then((res: any) => {
          resove(res as any);
        })
        .catch((error) => {
          rejected(error);
        });
      delete timer[key];
    }, 500);
  });
};
/**
 * 获取全部角色（租户隔离）
 * @param params
 */
export const getAllRolesList = (params) => defHttp.get({ url: Api.allRolesList, params });
/**
 * 获取全部租户
 */
export const getAllTenantList = (params) => defHttp.get({ url: Api.allTenantList, params });

/**
 * 回收站列表
 * @param params
 */
export const getRecycleUserList = (params) => defHttp.get({ url: Api.getRecycleUser, params });
/**
 * 回收站还原
 * @param params
 */
export const putRecycleUser = (params, handleSuccess) => {
  return defHttp.put({ url: Api.putRecycleUser, params }).then(() => {
    handleSuccess();
  });
};
/**
 * 回收站删除
 * @param params
 */
export const deleteRecycleBin = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.deleteRecycleBin, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
};
/**
 * 修改密码
 * @param params
 */
export const changePassword = (params) => {
  return defHttp.put({ url: Api.changePassword, params });
};
/**
 * 冻结解冻
 * @param params
 */
export const updateStatus = (params, handleSuccess) => {
  return defHttp.put({ url: Api.updateStatus, params }).then(() => {
    handleSuccess();
  });
};

/**
 * 用户离职列表
 * @param params
 */
export const getQuitList = (params) => {
  return defHttp.get({ url: Api.getQuitList, params });
};

/**
 * 取消离职
 * @param params
 */
export const putCancelQuit = (params, handleSuccess) => {
  return defHttp.put({ url: Api.putCancelQuit, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
};
